package pl.pkolodziej.flex.dao;

import java.sql.Timestamp;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import pl.pkolodziej.platform.vo.course.Course;
import pl.pkolodziej.platform.vo.course.UserCourseInfo;
import pl.pkolodziej.platform.vo.user.UserInfo;

@Repository
public class UserCourseDAOImpl implements UserCourseDAO {

	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	HibernateTemplate hibernateTemplate;
	
	public void updateUserCourse(String username,int course_id, Timestamp lastattempt,int pages, int score, int starttest, int endtest) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set lastattempt=:_lastattempt" +
				" ,score = :_score" +
				" ,starttest = :_starttest" +
				" ,endtest = :_endtest" +
				" ,pages = :_pages" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_lastattempt", lastattempt);
		hql.setParameter("_score", score);
		hql.setParameter("_starttest", starttest);
		hql.setParameter("_endtest", endtest);
		hql.setParameter("_pages", pages);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		
		hql.executeUpdate();
	}
	
	public void updateUserCourseLastattempt(String username,int course_id, Timestamp lastattempt) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set lastattempt=:_lastattempt" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_lastattempt", lastattempt);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void updateUserCourseScoreAndPage(String username, int course_id,int pages, int score) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" score = :_score" +
				" ,pages = :_pages" +
				" where username= :_username and course_id=:_course_id");

		hql.setParameter("_score", score);
		hql.setParameter("_pages", pages);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void updateUserCourseStartTest(String username, int course_id,int starttest, String data) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" starttest = :_starttest" +
				",starttest_state = :_data" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_starttest", starttest);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);
		hql.setParameter("_data", data);
		
		hql.executeUpdate();
	}
	
	public void updateUserCourseEndTest(String username, int course_id, int endtest, String data) {
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" endtest = :_endtest" +
				",endtest_state = :_data" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_endtest", endtest);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);
		hql.setParameter("_data", data);

		hql.executeUpdate();
	}
	
	public void startTestDone(String username, int course_id){
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" starttest_done = :_starttest_done" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_starttest_done", true);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void endTestDone(String username, int course_id){
		Query hql = sessionFactory.getCurrentSession().createQuery("update UserCourseInfo set" +
				" endtest_done = :_endtest_done" +
				" where username= :_username and course_id=:_course_id");
		
		hql.setParameter("_endtest_done", true);
		hql.setParameter("_username", username);
		hql.setParameter("_course_id", course_id);

		hql.executeUpdate();
	}
	
	public void addUserCourse(UserCourseInfo uci) {
		sessionFactory.getCurrentSession().save(uci);
	}
	
	public UserCourseInfo getUserCourseInfo(String username, int courseId){
		UserCourseInfo uci = (UserCourseInfo) hibernateTemplate.find("from UserCourseInfo where username = '"+username+"'and course_id="+courseId).get(0);
		return uci;
	}
	
	public Course getCourse(int courseId){
		Course c = (Course) hibernateTemplate.find("from Course where course_id = "+courseId).get(0);
		return c;
	}
	
	public UserInfo getUserInfo(String username){
		UserInfo ui= (UserInfo) hibernateTemplate.find("from UserInfo where username = '"+username+"'").get(0);
		return ui;
	}


}
